package com.flyme.module.base.provider.controller;

import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysCertificateRemoteService;
import com.flyme.module.base.client.dto.SysCertificateDto;
import com.flyme.module.base.client.entity.SysCertificate;
import com.flyme.module.base.client.entity.SysStudyconfig;
import com.flyme.module.base.provider.service.SysCertificateService;
import com.flyme.module.base.provider.service.SysStudyconfigService;
import com.flyme.module.file.client.entity.SysCourse;
import com.flyme.module.file.provider.service.SysCataLogService;
import com.flyme.module.file.provider.service.SysCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 证书管理
 * @author zyh
 * @date 2020-3-26
 */
@Api(tags = "证书")
@RestController
public class SysCertificateController extends BaseController<SysCertificateService, SysCertificate> implements SysCertificateRemoteService {

    @Autowired
    private SysCataLogService cataLogService;
    @Autowired
    private SysCourseService courseService;
    @Autowired
    private SysStudyconfigService studyconfigService;

    /**
     * 平台证书列表
     * @return
     */
    @Override
    public ResultBody list(PagerInfo pagerInfo, SysCertificate certificate) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("*, studyconfig.tagetPeriod");
        cq.like("certificateName", certificate.getCertificateName());
        cq.createAlias("studyconfig", "studyconfig");
        if(!certificate.getIndustry().equals(0L)){
            cq.eq("industry", certificate.getIndustry());
        }
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    /**
     * 编辑证书
     * @return
     */
    @ApiOperation(value = "编辑证书", notes = "编辑证书")
    @Override
    public ResultBody edit(SysCertificateDto certificate) {
        boolean result= bizService.saveOrUpdate(certificate);
        if(certificate.getIsStudy().equals(1)){
            SysStudyconfig config = new SysStudyconfig();
            config.setSyscertificateId(certificate.getId());
            config.setRegistrationFee(certificate.getRegistrationFee());
            config.setTagetPeriod(certificate.getTagetPeriod());
            config.setProofImage(certificate.getProofImage());
            config.setOfflinePeriod(certificate.getOfflinePeriod());
            config.setOncePeriod(certificate.getOncePeriod());
            if(ObjectUtils.isNotEmpty(certificate.getStudyconfigId())){
                config.setId(certificate.getStudyconfigId());
            }
            studyconfigService.saveOrUpdate(config);
        }
        return ResultBody.success(result);
    }


    /**
     * 查询课程
     * @return
     */
    @ApiOperation(value = "查询课程", notes = "查询课程")
    @Override
    public ResultBody selectCourse(SysCertificateDto certificate) {
        String[] cIds = certificate.getCourseIds().split(",");
        List<Map<String, String>> list = new ArrayList<>();
        for (String id : cIds) {
            Long userId = Long.valueOf(id);
            Map<String, String> data = new HashMap<>();
            SysCourse course = courseService.getById(userId);
            if (ObjectUtils.isNotEmpty(course)) {
                data.put("id", course.getId().toString());
                data.put("courseTitle", course.getCourseTitle());
            }
            list.add(data);
        }
        return ResultBody.success(list);
    }

    @Override
    public ResultBody selectCertificateList() {
        List<SysCertificate> list = bizService.list();
        List<Map> certificates = new ArrayList<>();
        for(SysCertificate c : list){
            Map<String, String> data = new HashMap<>();
            data.put("label", c.getCertificateName());
            data.put("value", c.getCertificateCode());
            data.put("id", c.getId().toString());
            certificates.add(data);
        }
        return ResultBody.success(certificates);
    }


}
